﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using plust.Clases;
using plust.Persistencia;
using System.Data;
using FirstFloor.ModernUI.Windows.Controls;

namespace plust.Content
{
    /// <summary>
    /// Interaction logic for DetalleFacturaDialog.xaml
    /// </summary>
    public partial class DetalleFacturaDialog : UserControl
    {
        private PersistenciaDB per = new PersistenciaDB();

        private Factura facturaObj;

        public DetalleFacturaDialog()
        {
            InitializeComponent();
        }
        public DetalleFacturaDialog(int id_venta, int id_cliente, int id_factura): this()
        {
            DataTable cliente = this.per.getCliente(id_cliente.ToString());
            DataTable prod = this.per.retornarProdxVenta(id_venta);
            DataTable factura = this.per.obtenerFactura(id_factura);

            this.lblNombre.Text = cliente.Rows[0]["nombre"].ToString();
            this.lblRut.Text = cliente.Rows[0]["rut"].ToString();
            this.lblGiro.Text = cliente.Rows[0]["giro"].ToString();
            this.lblDireccion.Text = cliente.Rows[0]["direccion"].ToString();
            this.lblCiudad.Text = cliente.Rows[0]["ciudad"].ToString();
            this.lblComuna.Text = cliente.Rows[0]["comuna"].ToString();

            this.lblMonto.Text = factura.Rows[0]["valor_neto"].ToString();
            this.lblIva.Text = (0.19 * int.Parse(factura.Rows[0]["valor_neto"].ToString())).ToString();
            this.lblTotal.Text = (int.Parse(factura.Rows[0]["valor_neto"].ToString()) + double.Parse(lblIva.Text)).ToString();

            this.dgProductos.ItemsSource = prod.DefaultView;

            this.dgPagos.ItemsSource = per.obternerPagos(id_factura.ToString()).DefaultView;

            this.facturaObj = per.obtenerFacturaObjeto(id_factura);

            if (this.per.verificarEstadoPago(this.facturaObj.id_factura))
            {
                this.btPagar.IsEnabled = false;
            }

        }

        private void btPagar_Click(object sender, RoutedEventArgs e)
        {
            NuevoPagoDialog npd = new NuevoPagoDialog(this.facturaObj);

            var wnd = new ModernWindow
            {
                Style = (Style)App.Current.Resources["EmptyWindow"],
                Content = npd,
                Width = 560,
                Height = 440,
                Margin = new Thickness(42),
                Padding = new Thickness(42),
                WindowStartupLocation = WindowStartupLocation.CenterScreen,
                ResizeMode = ResizeMode.NoResize
            };

            wnd.ShowDialog();

            this.dgPagos.ItemsSource = per.obternerPagos(facturaObj.id_factura.ToString()).DefaultView;

            if(this.per.verificarEstadoPago(this.facturaObj.id_factura)){
                this.btPagar.IsEnabled = false;
            }

        }
    }
}
